package com.xk.meeting.back.controller;

import com.xk.meeting.entity.Meeting;
import com.xk.meeting.service.MeetingService;
import com.xk.meeting.service.MeetingServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/back/addMeeting")
public class AddMeetingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        Map<String, String[]> meetingMap = request.getParameterMap();
        Meeting meeting = new Meeting();
        try {
            BeanUtils.populate(meeting,meetingMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        MeetingService meetingService = new MeetingServiceImpl();
        int count = meetingService.addMeeting(meeting);

        if(count > 0){
            request.getContextPath();
            request.setAttribute("msg","添加成功");
            request.getRequestDispatcher("addmeeting.jsp").forward(request,response);
        }else {
            response.sendRedirect("../errorPage.jsp");
        }

    }
}
